namespace DotNetNuke.UI.UserControls
{
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.Services.Localization;
    using DotNetNuke.UI.Utilities;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// HelpButtonControl is a user control that provides all the server code to display
    /// field level help button.
    /// </summary>
    /// <remarks>
    /// To implement help, the control uses the ClientAPI interface.  In particular
    /// the javascript function __dnn_Help_OnClick()
    /// </remarks>
    /// <history>
    /// [smehaffie]	12/8/2004	Created
    /// </history>
    /// -----------------------------------------------------------------------------
    public class HelpButtonControl : UserControl
    {
        private static List<WeakReference> __ENCList = new List<WeakReference>();
        //("cmdHelp")]
        private LinkButton _cmdHelp;
        private string _ControlName;
        private string _HelpKey;
        //("imgHelp")]
        private Image _imgHelp;
        //("lblHelp")]
        private Label _lblHelp;
        //("pnlHelp")]
        private Panel _pnlHelp;
        private string _ResourceKey;

       
        public HelpButtonControl()
        {
            base.Init += new EventHandler(this.Page_Init);
            base.Load += new EventHandler(this.Page_Load);
            List<WeakReference> list = __ENCList;
            lock (list)
            {
                __ENCList.Add(new WeakReference(this));
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// GetLocalizedText gets the localized text for the provided key
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="key">The resource key</param>
        /// <param name="ctl">The current control</param>
        /// <history>
        /// [cnurse]	9/8/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        private string GetLocalizedText(string key, Control ctl)
        {
            Control parentControl = ctl.Parent;
            if (parentControl is PortalModuleBase)
            {
                PortalModuleBase ctrl = (PortalModuleBase) parentControl;
                return DotNetNuke.Services.Localization.Localization.GetString(key, ctrl.LocalResourceFile);
            }
            if (parentControl is Address)
            {
                Address ctrl = (Address) parentControl;
                return DotNetNuke.Services.Localization.Localization.GetString(key, ctrl.LocalResourceFile);
            }
            return this.GetLocalizedText(key, parentControl);
        }

        private void imageClick(object sender, ImageClickEventArgs e)
        {
            this.pnlHelp.Visible = !this.pnlHelp.Visible;
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.InitializeComponent();
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	9/8/2004	Created
        /// [jhenning]	9/6/2005	Utilizingnew EnableMinMax function
        /// </history>
        /// -----------------------------------------------------------------------------
        private void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (ClientAPI.BrowserSupportsFunctionality(ClientAPI.ClientFunctionality.DHTML))
                {
                    DNNClientAPI.EnableMinMax(this.cmdHelp, this.pnlHelp, true, DNNClientAPI.MinMaxPersistanceType.None);
                }
                else if (!this.Page.IsPostBack)
                {
                    this.pnlHelp.Visible = false;
                }
                if (this._HelpKey == "")
                {
                    this._HelpKey = this._ResourceKey + ".Help";
                }
                string helpText = this.GetLocalizedText(this._HelpKey, this);
                if (helpText != "")
                {
                    this.HelpText = helpText;
                }
            }
            catch (Exception exception1)
            {
                
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
                
            }
        }

        protected virtual LinkButton cmdHelp
        {
           
            get
            {
                return this._cmdHelp;
            }
            
            set
            {
                this._cmdHelp = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// ControlName is the Id of the control that is associated with the label
        /// </summary>
        /// <value>A string representing the id of the associated control</value>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	9/8/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public string ControlName
        {
            get
            {
                return this._ControlName;
            }
            set
            {
                this._ControlName = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// HelpKey is the Resource Key for the Help Text
        /// </summary>
        /// <value>A string representing the Resource Key for the Help Text</value>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	9/8/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public string HelpKey
        {
            get
            {
                return this._HelpKey;
            }
            set
            {
                this._HelpKey = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// HelpText is value of the Help Text if no ResourceKey is provided
        /// </summary>
        /// <value>A string representing the Text</value>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	9/8/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public string HelpText
        {
            get
            {
                return this.lblHelp.Text;
            }
            set
            {
                this.lblHelp.Text = value;
                this.imgHelp.AlternateText = value;
                if (value == "")
                {
                    this.imgHelp.Visible = false;
                }
            }
        }

        protected virtual Image imgHelp
        {
           
            get
            {
                return this._imgHelp;
            }
            
            set
            {
                this._imgHelp = value;
            }
        }

        protected virtual Label lblHelp
        {
           
            get
            {
                return this._lblHelp;
            }
            
            set
            {
                this._lblHelp = value;
            }
        }

        protected virtual Panel pnlHelp
        {
           
            get
            {
                return this._pnlHelp;
            }
            
            set
            {
                this._pnlHelp = value;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// ResourceKey is the Resource Key for the Help Text
        /// </summary>
        /// <value>A string representing the Resource Key for the Label Text</value>
        /// <remarks>
        /// </remarks>
        /// <history>
        /// [cnurse]	9/8/2004	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public string ResourceKey
        {
            get
            {
                return this._ResourceKey;
            }
            set
            {
                this._ResourceKey = value;
            }
        }
    }
}

